package com.ruoyi.carton.controller;


import com.ruoyi.carton.domain.Box;
import com.ruoyi.carton.service.BoxService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/box")
public class BoxController extends BaseController {

    @Autowired
    private BoxService boxService;

    /**
     * 新增纸箱
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-carton:box:add')")
    @Log(title = "纸箱管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Box box) {
        boolean result = boxService.addBox(box);
        return toAjax(result);
    }

    /**
     * 删除纸箱
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-carton:box:remove')")
    @Log(title = "纸箱管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") String id) {
        boolean result = boxService.removeById(id);
        return toAjax(result);
    }

    /**
     * 修改纸箱信息
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-carton:box:edit')")
    @Log(title = "纸箱管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult update(@RequestBody Box box) {
        boolean result = boxService.updateById(box);
        return toAjax(result);
    }

    /**
     * 查询纸箱详情
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-carton:box:query')")
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id") String id) {
        Box box = boxService.getById(id);
        if (box == null) {
            return AjaxResult.error("未找到对应纸箱");
        }
        return success(box);
    }

    /**
     * 查询纸箱列表（支持分页）
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-carton:box:list')")
    @GetMapping("/list")
    public TableDataInfo list(Box box) {
        startPage();
        List<Box> list = boxService.selectBoxList(box); // 请确保在Service实现该方法
        return getDataTable(list);
    }

}
